using UnityEngine;
using UnityEditor;
using Mirror;
using System.IO;

/// <summary>
/// 编辑器工具类，提供帮助方法修复NetworkManager设置和预制体问题
/// </summary>
public class PresetupHelper : EditorWindow
{
    private GameObject playerPrefab;
    private NetworkManager networkManager;
    
    [MenuItem("工具/网络设置修复工具")]
    public static void ShowWindow()
    {
        GetWindow<PresetupHelper>("网络设置修复工具");
    }
    
    private void OnGUI()
    {
        GUILayout.Label("NetworkManager 设置修复工具", EditorStyles.boldLabel);
        EditorGUILayout.Space();
        
        // 找到场景中的NetworkManager
        if (networkManager == null)
        {
            networkManager = FindObjectOfType<NetworkManager>();
        }
        
        // 显示当前NetworkManager信息
        if (networkManager != null)
        {
            EditorGUILayout.LabelField("找到NetworkManager", networkManager.name);
            EditorGUILayout.ObjectField("当前PlayerPrefab:", networkManager.playerPrefab, typeof(GameObject), false);
        }
        else
        {
            EditorGUILayout.HelpBox("场景中未找到NetworkManager，请先创建!", MessageType.Warning);
        }
        
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Player预制体设置", EditorStyles.boldLabel);
        
        // 从Resources加载预制体
        if (playerPrefab == null)
        {
            playerPrefab = Resources.Load<GameObject>("Prefabs/Player");
        }
        
        playerPrefab = (GameObject)EditorGUILayout.ObjectField("Player预制体:", playerPrefab, typeof(GameObject), false);
        
        if (playerPrefab != null)
        {
            NetworkIdentity identity = playerPrefab.GetComponent<NetworkIdentity>();
            if (identity != null)
            {
                EditorGUILayout.LabelField("NetworkIdentity状态:", "已找到");
                if (string.IsNullOrEmpty(GetAssetGUID(playerPrefab)))
                {
                    EditorGUILayout.HelpBox("预制体GUID为空，这可能导致NetworkIdentity组件无法正确初始化。", MessageType.Error);
                }
            }
            else
            {
                EditorGUILayout.HelpBox("Player预制体缺少NetworkIdentity组件!", MessageType.Error);
                if (GUILayout.Button("添加NetworkIdentity组件"))
                {
                    playerPrefab.AddComponent<NetworkIdentity>();
                    EditorUtility.SetDirty(playerPrefab);
                }
            }
        }
        
        EditorGUILayout.Space();
        
        // 修复按钮
        if (GUILayout.Button("自动修复PlayerPrefab设置"))
        {
            FixPlayerPrefab();
        }
        
        if (networkManager != null && playerPrefab != null && GUILayout.Button("设置NetworkManager的PlayerPrefab"))
        {
            networkManager.playerPrefab = playerPrefab;
            EditorUtility.SetDirty(networkManager);
        }
        
        EditorGUILayout.Space();
        EditorGUILayout.HelpBox("本工具可以帮助解决以下错误:\n- 'PlayerPrefab未设置'\n- 'Player Prefab must have a NetworkIdentity'", MessageType.Info);
    }
    
    private void FixPlayerPrefab()
    {
        if (playerPrefab == null)
        {
            EditorUtility.DisplayDialog("错误", "未找到Player预制体，请确保存在于Resources/Prefabs目录", "确定");
            return;
        }
        
        bool modified = false;
        
        // 检查并添加NetworkIdentity组件
        NetworkIdentity identity = playerPrefab.GetComponent<NetworkIdentity>();
        if (identity == null)
        {
            identity = playerPrefab.AddComponent<NetworkIdentity>();
            modified = true;
        }
        
        // 设置NetworkManager的PlayerPrefab
        if (networkManager != null && networkManager.playerPrefab == null)
        {
            networkManager.playerPrefab = playerPrefab;
            EditorUtility.SetDirty(networkManager);
            modified = true;
        }
        
        // 如果做了修改，保存资源
        if (modified)
        {
            PrefabUtility.SavePrefabAsset(playerPrefab);
            EditorUtility.SetDirty(playerPrefab);
            AssetDatabase.SaveAssets();
            EditorUtility.DisplayDialog("完成", "Player预制体已修复，NetworkIdentity组件已添加并设置。", "确定");
        }
        else
        {
            EditorUtility.DisplayDialog("信息", "Player预制体设置已经正确，无需修复。", "确定");
        }
    }
    
    // 获取资源的GUID
    private string GetAssetGUID(Object asset)
    {
        string path = AssetDatabase.GetAssetPath(asset);
        return AssetDatabase.AssetPathToGUID(path);
    }
} 